/*
 * @lc app=leetcode.cn id=225 lang=typescript
 *
 * [225] 用队列实现栈
 */

// @lc code=start

//  思路：
//  把队列前面的都取出来再加入队尾，让之前的队尾元素排到队头，这样就可以取出

//  复杂度：O(n) O(n) 

class MyStack {
    queue: number[]
    topItem: number

    constructor() {
        this.queue = []
        this.topItem = 0
    }

    push(x: number): void {
        this.queue.push(x)
        this.topItem = x
    }

    pop(): number {
        let len = this.queue.length
        while (len > 2) {
            this.queue.push(this.queue.shift()!)
            len--
        }
        // 记录新的队尾元素
        this.topItem = this.queue[0]
        this.queue.push(this.queue.shift()!)
        return this.queue.shift()!
    }

    top(): number {
        return this.topItem
    }

    empty(): boolean {
        return !this.queue.length
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * var obj = new MyStack()
 * obj.push(x)
 * var param_2 = obj.pop()
 * var param_3 = obj.top()
 * var param_4 = obj.empty()
 */
// @lc code=end

